<?php

namespace app\adminapi\controller;

use think\exception\ValidateException;
use think\Request;
use \app\common\business\Auth as authBs;
class Auth  extends BaseApi
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //接受keyword type
        $params = input();


        //type=tree 返回父子级树状结构 不等于tree返回无线级分类列表
        $result = (new authBs())->authList($params);
        if(empty($result)){
            $this->ok();
        }
        $this->ok($result);


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //新建权限数据
        $params = input();
        try {
            validate(\app\adminapi\validate\Auth::class)->check($params);
        }catch (ValidateException $e){
            $this->fail($e->getMessage());
        }
        try{
            $result = (new authBs())->authSave($params);
        }catch(\Exception $e){
            $this->fail($e->getMessage());
        }
        $this->ok($result);

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //获取权限详情
        $result = (new authBs())->authRead($id);

        if(empty($result)){
             $this->ok();
        }
        $this->ok($result);

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //权限更新
        $params = input();
        if(empty($id)){
            $this->fail('修改失败');
        }
        try {
            validate(\app\adminapi\validate\Auth::class)->check($params);
        }catch (ValidateException $e){
            $this->fail($e->getMessage());
        }

        $result = (new authBs())->authUpdate($id,$params);
        if(!$result){
            $this->fail('修改失败');
        }
        $this->ok($result);

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //权限删除
        if(empty($id)){
            $this->fail('删除失败');
        }
        $result = (new authBs())->authDelete($id);
        if(!$result){
             $this->fail('有子权限，无法删除');
        }
         $this->ok();
    }

    /**
     * 获取菜单权限
     */
    public function nav()
    {
        $result = (new authBs())->getNavList();
        if(!$result){
            $this->ok();
        }
        $this->ok($result);
    }



}